//
//  ProductInfoTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/26.
//

import UIKit

class ProductInfoTableViewCell: UITableViewCell {
    var nameLab: UILabel!
    var idLab: UILabel!
    var specificationView: UIView!
    var specificationLab: UILabel!
    var selectSecpBtn: UIButton!
    var model: ProductDetailDataModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        self.nameLab = UILabel(frame: .zero)
        self.nameLab.textColor = Theme.titleLabColor
        self.nameLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.nameLab.textAlignment = .left
        self.nameLab.numberOfLines = 0
        self.contentView.addSubview(self.nameLab)
        
        
        self.idLab = UILabel(frame: .zero)
        self.idLab.textColor = UIColor(hex: 0x939AA8)
        self.idLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.idLab.textAlignment = .left
        self.contentView.addSubview(self.idLab)
        
        self.specificationView = UIView(frame: .zero)
        self.specificationView.layer.cornerRadius = 8
        self.specificationView.layer.masksToBounds = true
        self.specificationView.layer.borderWidth = 1
        self.specificationView.layer.borderColor = UIColor(hex: 0x939AA8).cgColor
        self.contentView.addSubview(self.specificationView)
        
        
        self.specificationLab = UILabel(frame: .zero)
        self.specificationLab.textColor = Theme.titleLabColor
        self.specificationLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.specificationLab.textAlignment = .left
        self.specificationView.addSubview(self.specificationLab)
        
        self.selectSecpBtn = UIButton(frame: .zero)
        self.selectSecpBtn.setTitle(MMLocalizedString("select_specification", comment: "选择规格"), for: .normal)
        self.selectSecpBtn.setTitle(MMLocalizedString("select_specification", comment: "选择规格"), for: .highlighted)
        self.selectSecpBtn.setImage(UIImage(named: "icon_news_arrows"), for: .normal)
        self.selectSecpBtn.semanticContentAttribute = .forceRightToLeft
        self.selectSecpBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 12)
        self.selectSecpBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.selectSecpBtn.addTarget(self, action: #selector(selectSecpBtnAntion), for: .allEvents)
        self.specificationView.addSubview(self.selectSecpBtn)
        
        self.addLayout()
    }
    
    
    
    func addLayout(){
        
        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
        }
        
        self.idLab.snp.makeConstraints { make in
            make.top.equalTo(self.nameLab.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        self.specificationView.snp.makeConstraints { make in
            make.top.equalTo(self.idLab.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(40)
            make.bottom.equalTo(-10)
        }
        
        self.specificationLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(10)
            make.height.equalTo(40)
        }
        
        self.selectSecpBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(-10)
            make.height.equalTo(40)
        }
        
    }
    
    
    //setModel
    func setModel(model: ProductDetailDataModel){
        self.model = model
        self.nameLab.text = model.name ?? ""
        self.idLab.text = MMLocalizedString("identification", comment: "产品编号") + (model.productNo ?? "")
        self.specificationLab.text = MMLocalizedString("specification", comment: "产品编号")
        self.specificationView.snp.updateConstraints { make in
            make.top.equalTo(self.idLab.snp.bottom).offset(10)
            make.height.equalTo(40)
            make.bottom.equalTo(-10)
        }
        //单规格
        if (model.parameters ?? []).count == 0{
            self.specificationView.snp.updateConstraints { make in
                make.top.equalTo(self.idLab.snp.bottom).offset(0)
                make.height.equalTo(0)
                make.bottom.equalTo(-0)
            }
        }
        
        self.updateConstraints()
    }
    
   
    //MARK: - Antion
    
    //选择规格
    @objc func selectSecpBtnAntion(){
        self.model?.selectSpecificationBlock?()
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
